
function initPage(baseInfoJson,infusionBloodInfoJson){
	var basePath = $("#basePath").val();
	
	
	$("select:not([data-address])").each(function(){
		$(this).append("<option value='' >---请选择---</option>");
		
	});
	var bloodTypes =  baseInfoJson['bloodTypes'];
	$("select[name$='infusionBloodTypeCode']").each(function(){
		var options = "";
		for(var index in bloodTypes){
			var bloodType = bloodTypes[index];
	    	options+="<option value='"+bloodType.uniq_code+"'>"+bloodType.label+"</option>"
	    }
		$(this).append(options);
	});
	
	$("select[name$='.bleedingReactionTypeCode']").each(function(){
		var options = "";
		
	    options+="<option value='1'>输血后空气栓塞</option>";
	    options+="<option value='2'>输血后血管并发症</option>";
	    options+="<option value='3'>输血后感染</option>";
	    options+="<option value='4'>ABO血型不配合性反应</option>";
	    options+="<option value='5'>Rh不配合性反应</option>";
	    options+="<option value='6'>血清过敏性休克</option>";
	    options+="<option value='7'>血清反应性荨麻疹</option>";
		$(this).append(options);
	});
	
	
	
	var validator = $("#infusionBloodInfoForm").validate({
		errorElement: "title",
		submitHandler:function(form){
			layer.load(1);
			return true;
		},
		rules:{
			
			infusionBloodTimes: {
            	range:[1,500]
            },
            infusionBloodReactTimes: {
            	range:[1,500]
            },
            //inpatientTransfusionsRecords.transfusionElementVolume:{
//            	required:true
//            }
		}
	});
	var infusionBloodRecords = infusionBloodInfoJson['infusionBloodRecords'];
	if(infusionBloodRecords == undefined || infusionBloodRecords.length ==0){
		addInfusionBloodRecord();
	}else{
		for(var index in infusionBloodRecords){
			var infusionBloodRecord = infusionBloodRecords[index];
	    	addInfusionBloodRecordByRecord(infusionBloodRecord);
	    }
	}
	
	
	//住院输血清单
	initInpatientTransfusionsPage(baseInfoJson,infusionBloodInfoJson);

}
//住院输血清单
function initInpatientTransfusionsPage(baseInfoJson,infusionBloodInfoJson){
	
	var bloodTransfusionMeasurementUnits =  baseInfoJson['bloodTransfusionMeasurementUnit'];
	$("select[name$='bloodTransfusionMeasurementUnitCode']").each(function(){
		var options = "";
		for(var index in bloodTransfusionMeasurementUnits){
			var bloodTransfusionMeasurementUnit = bloodTransfusionMeasurementUnits[index];
	    	options+="<option value='"+bloodTransfusionMeasurementUnit.uniq_code+"'>"+bloodTransfusionMeasurementUnit.label+"</option>"
	    }
		$(this).append(options);
	});
	var bloodTransfusionVarieties =  baseInfoJson['bloodTransfusionVarieties'];
	var $bloodTransfusionVarietiesCode = $("select[name$='bloodTransfusionVarietiesCode']");
	$bloodTransfusionVarietiesCode.each(function(){
		var options = "";
		for(var index in bloodTransfusionVarieties){
			var bloodTransfusionVarietie = bloodTransfusionVarieties[index];
	    	options+="<option value='"+bloodTransfusionVarietie.uniq_code+"' defaultUnit='"+bloodTransfusionVarietie.defaultunit+"'>"+bloodTransfusionVarietie.label+"</option>"
	    }
		$(this).append(options);
	});
//	var j = $("#inpatientTransfusionsTable").find("input[name$='.transfusionElementVolume']").validate({number:true});
	
	//判断是否已经有数据， 没有创建空模板； 有：渲染模板
	var inpatientTransfusionsRecords = infusionBloodInfoJson['inpatientTransfusionsRecords'];
	if(inpatientTransfusionsRecords == undefined || inpatientTransfusionsRecords.length ==0){
		addInfusionBloodRecord();
	}else{
//		debugger;
		for(var index in inpatientTransfusionsRecords){
			var inpatientTransfusionsElement = inpatientTransfusionsRecords[index];
			addInpatientTransfusionsRecordByRecord(inpatientTransfusionsElement);
	    }
	}
}

function addInfusionBloodRecord(){
	var basePath = $("#basePath").val();
	var infusionBloodCountOjb = $("#infusionBloodRecordCount");
	var count = parseInt(infusionBloodCountOjb.val());
	var add_content=$("#templateInfusionBloodTr").clone();
	add_content.removeAttr("id");
	
	add_content.find("input[name^='infusionBloodRecords[0].']").each(function(){
		var eventObj = $(this);
		eventObj.attr("name",eventObj.attr("name").replace("infusionBloodRecords[0]", "infusionBloodRecords["+count+"]"));
   		
	});
	
	add_content.find("select[name^='infusionBloodRecords[0].']").each(function(){
		var eventObj = $(this);
		eventObj.attr("name",eventObj.attr("name").replace("infusionBloodRecords[0]", "infusionBloodRecords["+count+"]"));
	});
	
	add_content.find("select").each(function(){
		$(this).change(function(){
	   		var eventObj = $(this);
	   		var nameObjName = eventObj.attr("name").replace("Code", "Name");
	   		
	   		if(eventObj.val()!=""){
	   			var name=eventObj.find("option:selected").text();
	   			eventObj.next("input[name='"+nameObjName+"']").val(name);
	   		}else{
	   			eventObj.next("input[name='"+nameObjName+"']").val("");
	   		}
	   	});
	});
	
	add_content.find("input[name$='.isBleedingReactionCode']").each(function(){
		$(this).click(function(){
			 
			 $(this).nextAll("input[name='"+$(this).attr("name").replace("Code", "Name")+"']").val($(this).attr("title"));
		 });
		 $(this).dblclick(function(){
			$(this).removeAttr("checked");
			var title = $('input:radio[name="isBleedingReactionCode"]:checked').attr("title");
			$(this).nextAll("input[name='"+$(this).attr("name").replace("Code", "Name")+"']").val(title);
		});
	});
	
	
	
	
	
	$("#infusionBloodRecords tbody").append(add_content);
	infusionBloodCountOjb.val(count +1);
	return add_content;
}

function deleteInfusionBloodRecord(obj){
	$(obj).parent().parent().remove();
}

function addInfusionBloodRecordByRecord(infusionBloodRecord){
	var add_content = addInfusionBloodRecord();
	if(infusionBloodRecord.infusionBloodTypeCode){
		
		var selectOption = add_content.find("select[name$='.bloodTransfusionVarietiesCode']").find("option[value='"+infusionBloodRecord.infusionBloodTypeCode+"']")
		if(selectOption.length > 0){
			selectOption.attr("selected",true);
		}else{
			add_content.find("select[name$='.infusionBloodTypeCode']").append("<option selected='selected' value='"+infusionBloodRecord.infusionBloodTypeCode+"'>"+infusionBloodRecord.infusionBloodTypeName+"</option>");
		}
	}
	add_content.find("input[name$='.infusionBloodTypeName']").val(infusionBloodRecord.infusionBloodTypeName);
	if(infusionBloodRecord.isBleedingReactionCode){
		add_content.find("input[name$='.isBleedingReactionCode'][value='"+infusionBloodRecord.isBleedingReactionCode+"']").attr("checked",true);
	}
	add_content.find("input[name$='.isBleedingReactionName']").val(infusionBloodRecord.isBleedingReactionName);
	add_content.find("input[name$='.bleedingDateTime']").val(infusionBloodRecord.bleedingDateTime);
	var infusionBloodElements = infusionBloodRecord.infusionBloodElements
	for(var index in infusionBloodElements){
		var infusionBloodElement = infusionBloodElements[index];
		add_content.find("input[name$='.infusionBloodElements["+index+"].bloodElementVolume']").val(infusionBloodElement.bloodElementVolume);
		add_content.find("input[name$='.infusionBloodElements["+index+"].bloodElementCode']").val(infusionBloodElement.bloodElementCode);
		add_content.find("input[name$='.infusionBloodElements["+index+"].bloodElementName']").val(infusionBloodElement.bloodElementName);
		add_content.find("input[name$='.infusionBloodElements["+index+"].bloodElementUnitCode']").val(infusionBloodElement.bloodElementUnitCode);
		add_content.find("input[name$='.infusionBloodElements["+index+"].bloodElementUnitName']").val(infusionBloodElement.bloodElementUnitName);
    }
	
	
	
	if(infusionBloodRecord.bleedingReactionTypeCode){
		
		var selectOption = add_content.find("select[name$='.bleedingReactionTypeCode']").find("option[value='"+infusionBloodRecord.bleedingReactionTypeCode+"']")
		if(selectOption.length > 0){
			selectOption.attr("selected",true);
		}else{
			add_content.find("select[name$='.bleedingReactionTypeCode']").append("<option selected='selected' value='"+infusionBloodRecord.bleedingReactionTypeCode+"'>"+infusionBloodRecord.bleedingReactionTypeName+"</option>");
		}
	}
	add_content.find("input[name$='.bleedingReactionTypeName']").val(infusionBloodRecord.bleedingReactionTypeName);
	
	var j = $("#inpatientTransfusionsTable").find("input[name$='.transfusionElementVolume']").validate({number:true});
}

/*
住院输血清单 start
*/

function addInpatientTransfusionsRecord(){
	var basePath = $("#basePath").val();
	var inpatientTransfusionsCount = $("#inpatientTransfusionsCount");
	var count = parseInt(inpatientTransfusionsCount.val());
	var add_content=$("#templateInpatientTransfusionsTr").clone();
	add_content.removeAttr("id");
	
	add_content.find("input[name^='inpatientTransfusionsRecords[0].']").each(function(){
		var eventObj = $(this);
		eventObj.attr("name",eventObj.attr("name").replace("inpatientTransfusionsRecords[0]", "inpatientTransfusionsRecords["+count+"]"));
   		
	});
	
	add_content.find("select[name^='inpatientTransfusionsRecords[0].']").each(function(){
		var eventObj = $(this);
		eventObj.attr("name",eventObj.attr("name").replace("inpatientTransfusionsRecords[0]", "inpatientTransfusionsRecords["+count+"]"));
	});
	
	add_content.find("select").each(function(){
		$(this).change(function(){
	   		var eventObj = $(this);
	   		console.log(eventObj.attr("name"));
	   		var eventObjName = eventObj.attr("name");
	   		
	   		var nameObjName = eventObj.attr("name").replace("Code", "Name");
	   		
	   		if(eventObj.val()!=""){
	   			var name=eventObj.find("option:selected").text();
	   			eventObj.next("input[name='"+nameObjName+"']").val(name);
	   		}else{
	   			eventObj.next("input[name='"+nameObjName+"']").val("");
	   		}
	   		
	   		//如果是输血品种，设置输血计量单位默认值
	   		if(".bloodTransfusionVarietiesCode" == eventObjName.substr(eventObjName.lastIndexOf("."), eventObjName.length)){
	   			var selectedOption = eventObj.find("option:selected");
	   			console.log(selectedOption);
	   			var defaultunit = $(selectedOption).attr("defaultunit");
	   			if(undefined == defaultunit)defaultunit = "";
	   			var bloodTransfusionMeasurementUnitCode = eventObj.parent().parent().find("select[name$='bloodTransfusionMeasurementUnitCode']");
	   			bloodTransfusionMeasurementUnitCode.find("option[value='"+defaultunit+"']").prop("selected", true).siblings().remove("selected");
	   			eventObj.parent().parent().find("input[name$='bloodTransfusionMeasurementUnitName']").val(defaultunit);
	   		};
	   	});
	});
	
	
	
	$("#inpatientTransfusionsTable tbody").append(add_content);
	inpatientTransfusionsCount.val(count +1);
	return add_content;
}

function deleteInpatientTransfusionsRecord(obj){
	$(obj).parent().parent().remove();
}
function addInpatientTransfusionsRecordByRecord(inpatientTransfusionsElement){
	var add_content = addInpatientTransfusionsRecord();
	
	if(inpatientTransfusionsElement.bloodTransfusionMeasurementUnitCode){
		
		var selectOption = add_content.find("select[name$='.bloodTransfusionMeasurementUnitCode']").find("option[value='"+inpatientTransfusionsElement.bloodTransfusionMeasurementUnitCode+"']")
		if(selectOption.length > 0){
			selectOption.attr("selected",true);
		}else{
			add_content.find("select[name$='.bloodTransfusionMeasurementUnitCode']").append("<option selected='selected' value='"+inpatientTransfusionsElement.bloodTransfusionMeasurementUnitCode+"'>"+inpatientTransfusionsElement.bloodTransfusionMeasurementUnitName+"</option>");
		}
	}
	add_content.find("input[name$='.bloodTransfusionMeasurementUnitName']").val(inpatientTransfusionsElement.bloodTransfusionMeasurementUnitName);
	
	if(inpatientTransfusionsElement.bloodTransfusionVarietiesCode){
		
		var selectOption = add_content.find("select[name$='.bloodTransfusionVarietiesCode']").find("option[value='"+inpatientTransfusionsElement.bloodTransfusionVarietiesCode+"']")
		if(selectOption.length > 0){
			selectOption.attr("selected",true);
		}else{
			add_content.find("select[name$='.bloodTransfusionVarietiesCode']").append("<option selected='selected' value='"+inpatientTransfusionsElement.bloodTransfusionVarietiesCode+"'>"+inpatientTransfusionsElement.bloodTransfusionVarietiesName+"</option>");
		}
	}
	add_content.find("input[name$='.bloodTransfusionVarietiesName']").val(inpatientTransfusionsElement.bloodTransfusionVarietiesName);
	
	var transfusionElementVolume = inpatientTransfusionsElement.transfusionElementVolume
	add_content.find("input[name$='.transfusionElementVolume']").val(transfusionElementVolume);
	
}
/*
住院输血清单 end
*/